package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.MyCollectionBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/7/16 0016.
 */

public class MyCollectionModel implements MyCollectionModelImpl {

    private static final String                 TAG = "MyCollectionModel";
    private OnRequestListener                   mListener;

    public MyCollectionModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getCollectionResult(String id) {
        Observable<MyCollectionBean> result = DataManager.getMyCollectionResult(
                id
        );
        Observable<MyCollectionBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<MyCollectionBean>() {

            @Override
            public void onNext(@NonNull MyCollectionBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getDelResult(String uid, String id) {
        Observable<CommonBean> result = DataManager.getMyCollectionDelResult(
                uid,
                id
        );
        Observable<CommonBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean bean) {
                if (mListener != null) {
                    mListener.onDelSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener {

        void onSuccess(MyCollectionBean bean);

        void onError(Throwable throwable);

        void onDelSuccess(CommonBean bean);
    }
}
